<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/27
 * Time: 15:58
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class  Evaluate extends Base{
    public function index(){
        if (request()->isAjax()) {
            extract(input());
            $map =array();
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_real_name|uuu.user_username'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($keyword1)&&$keyword1!="")
            {
                $map['uuu.user_phone'] = ['like',"%" . $keyword1 . "%"];
            }
            if(isset($keyword2)&&$keyword2!="")
            {
                $map['oe.evaluate_content'] = ['like',"%" . $keyword2 . "%"];
            }
            if(isset($status)&&$status!="")
            {
                $map['oe.evaluate_type'] = $status;
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 50;
            $count = Db::name('order_evaluate')->alias('oe')
                ->join('up_u_user uuu','uuu.user_id = oe.evaluate_p_user_id','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('order_evaluate')->alias('oe')
                ->join('up_u_user uuu','uuu.user_id = oe.evaluate_p_user_id','left')
                ->page($NowPage, $limits)
                ->where($map)
                ->field('oe.*,uuu.user_username,uuu.user_real_name,uuu.user_phone')
                ->order('oe.evaluate_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                    $user_role =Db::table('up_u_user')->where('user_id',$vo['evaluate_b_user_id'])->value('user_role');
                    if($user_role==1){
                        $vo['role'] = '司机';
                    }else{
                        $vo['role'] = '加盟商';
                    }
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    //编辑
    public function update(){
        if(Request::instance()->isPost()){
            $data['evaluate_star'] = input('param.evaluate_star');
            $data['evaluate_content'] = input('param.content');
            $res = Db::table('up_order_evaluate')->where('evaluate_id',input('param.id'))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $id = input('param.evaluate_id');
        $res = Db::table('up_order_evaluate')->where('evaluate_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function detail(){
        $id = input('param.evaluate_id');
        $info = Db::name('order_evaluate')->alias('oe')
            ->join('up_u_user uuu','uuu.user_id = oe.evaluate_p_user_id','left')
            ->where('oe.evaluate_id',$id)
            ->field('oe.*,uuu.user_username,uuu.user_real_name,uuu.user_phone')
            ->find();
        $user_role =Db::table('up_u_user')->where('user_id',$info['evaluate_b_user_id'])->value('user_role');
        if($user_role==1){
            $info['role'] = '司机';
        }else{
            $info['role'] = '加盟商';
        }
        $info['evaluate_img'] = explode(',',$info['evaluate_img']);
        $this->assign('res',$info);
        return $this->fetch();

    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('order_evaluate')->where('evaluate_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    public function order(){
        $id = input('param.id');
        $map['a.order_id'] = $id;
        $info = Db::name('order')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
            ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
            ->where($map)
            ->field('a.*,uuu.user_real_name,uuu.user_phone,uuu1.user_gongsi
            ,uuu1.user_username as qy_username,uuu1.user_phone as qy_phone,
             ua.district as go_sheng,ua1.district as go_shi,
             ua2.district as to_sheng,ua3.district as to_shi')
            ->find();
        $info['go_address'] = $info['go_sheng'].'-'.$info['go_shi'];
        $info['to_address'] = $info['to_sheng'].'-'.$info['to_shi'];
        $info['zxlx_name'] = Db::table('up_car_zxlx')->where('zxlx_id',$info['zxlx_id'])->value('zxlx_name');
        $info['hwlx_name'] = Db::table('up_car_hwlx')->where('hwlx_id',$info['hwlx_id'])->value('hwlx_name');
        $this->assign('res',$info);
        return $this->fetch();
    }
}